package com.ynny.librarysystemmanager.controller;

import com.github.pagehelper.PageInfo;
import com.ynny.librarysystemmanager.common.R;
import com.ynny.librarysystemmanager.entity.User;
import com.ynny.librarysystemmanager.entity.UserDTO;
import com.ynny.librarysystemmanager.mapper.UserMapper;
import com.ynny.librarysystemmanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    //查询所有 应该是带分页的
    //分页查询

    @GetMapping("/list")
    public R list(@RequestBody User user){

        List<User> users = userService.findAll(user);

        return R.ok(users);

    }

    @GetMapping("/page")
    public R page(@RequestBody UserDTO dto){

        PageInfo page = userService.page(dto);

        return R.ok(page);

    }


    @PostMapping("/edit")
    public R edit(@RequestBody User user){

        //修改逻辑
        if (user.getUserId() != null){
            int i = userService.editUser(user);
            return R.ok(i);
        }else {
            int i = userService.addUser(user);
            return R.ok(i);
        }


    }

    //是否禁用   1 禁用  0 正常

    @DeleteMapping("/disable/{userId}/{status}")
    public R delete(@PathVariable ("userId") Integer userId,
                    @PathVariable ("status") Integer status){
        int i = userService.disableUser(userId , status);
        return R.ok(i);
    }


}
